//////////////////////////////////////////ok
#include"stdafx.h"

#include "bochs.h"



void IA32_CPU::JCXZ_Jb(Ia32_Instruction_c *i)
{

    Bit32u temp_ECX;
    if (i->as32L())
      temp_ECX = ECX;
    else
      temp_ECX = CX;

    if ( temp_ECX == 0 ) 
	{
      Bit32u new_EIP = EIP + (Bit32s) i->Id();
      if (i->os32L()==0) new_EIP &= 0x0000ffff;
      branch_near32(new_EIP);
    }
}

void IA32_CPU::LOOPNE_Jb(Ia32_Instruction_c *i)
{

    Bit32u count;
    if (i->as32L())
      count = ECX;
    else
      count = CX;

    count--;
    if ( (count!=0) && (get_ZF()==0) ) 
	{
      Bit32u new_EIP = EIP + (Bit32s) i->Id();
      if (i->os32L()==0) new_EIP &= 0x0000ffff;
      branch_near32(new_EIP);
    }

	if (i->as32L())
      ECX = ECX - 1;  // zero extend
    else
      CX--;
 
}

void IA32_CPU::LOOPE_Jb(Ia32_Instruction_c *i)
{

	Bit32u count;
	if (i->as32L())
		count = ECX;
	else
		count = CX;

	count--;

	if ( (count!=0) && get_ZF()) 
	{
		Bit32u new_EIP = EIP + (Bit32s) i->Id();
		if (i->os32L()==0) new_EIP &= 0x0000ffff;
		branch_near32(new_EIP);
	}
		
	if (i->as32L())
		ECX = ECX - 1;   // zero extend
	else
		CX--;
  
}

void IA32_CPU::LOOP_Jb(Ia32_Instruction_c *i)
{
  
    Bit32u count;
	Bit32u fuckeip=EIP + (Bit32s) i->Id();
	if(fuckeip==EIP-2)
	{	
		ECX=0;
		return;
	}

    if (i->as32L())
      count = ECX;
    else
      count = CX;

    count--;
    
	if (count != 0) 
	{
      Bit32u new_EIP = EIP + (Bit32s) i->Id();
      if (i->os32L()==0) 
		  new_EIP &= 0x0000ffff;
      branch_near32(new_EIP);
    }
   
	if (i->as32L())
      ECX = ECX - 1;         // zero extend
    else
      CX--;
  
}
